package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.UnreadMessageEntity;
import com.zw.yzk.kabu.data.network.task.Task;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.view.view.MessageCenterView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/4.
 */

public class MessageCenterPresenter implements Presenter {

    private UseCase getUnreadCount;
    private AppCompatActivity activity;
    private MessageCenterView messageView;

    @Inject
    public MessageCenterPresenter(@Named("getUnreadCountUseCase") UseCase getUnreadCount,
                                  AppCompatActivity activity) {
        this.getUnreadCount = getUnreadCount;
        this.activity = activity;
    }

    public void setView(MessageCenterView view) {
        this.messageView = view;
    }

    public void getUnreadCount() {
        getUnreadCount.execute(new GetUnreadCountSubscriber(activity), new Task());
    }


    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getUnreadCount.unSubscribe();
    }

    private class GetUnreadCountSubscriber extends BaseSubscriber<UnreadMessageEntity> {

        public GetUnreadCountSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onNext(UnreadMessageEntity entity) {
            messageView.setLightCount(entity.result.praiseCount);
            messageView.setCommentCount(entity.result.commentCount + entity.result.replyCount);
        }

        @Override
        public String addErrorMessage() {
            return "";
        }
    }

}
